package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.operatingunitform;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.google.common.base.Strings;
import com.kingyun.gpsinspection.purificationservice.common.enums.DataItemTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.*;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.OperatingUnitFormItemMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.WorkCellInfoMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.operatingunitform.JsmOperatingUnitFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by zmling on 2016/12/19.
 * 操作表单信息 ， {操作单元 或者 操作表单 }
 */

public class JsmOperatingUnitFormServiceImpl implements JsmOperatingUnitFormService {

    @Autowired
    private OperatingUnitFormMapper operatingUnitFormMapper;
    @Autowired
    private OperatingUnitFormFatherMapper operatingUnitFormFatherMapper;

    @Autowired
    private com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.OperatingUnitFormFatherMapper jsmoperatingUnitFormFatherMapper;


    @Autowired
    private OperatingUnitFormItemMapper operatingUnitFormItemMapper;

    @Autowired
    private JsmOperatingUnitFormItemMapper jsmJsmOperatingUnitFormItemMapper;

    @Autowired
    private CheckboxTypeMapper checkboxTypeMapper;
    @Autowired
    private RadioTypeMapper radioTypeMapper;
    @Autowired
    private WorkCellInfoMapper workCellInfoMapper;
    @Autowired
    private JsmWorkCellInstanceMapper jsmWorkCellInstanceMapper;


    /**
     * 新增配置工作表单{一站一案 和 操作单元}
     *
     * @param map
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addOperatingUnitForm(Map map, Employee employee) throws Exception {
        boolean cloneData=false;
        Integer resInt = 0;
        OperatingUnitForm operatingUnitForm = new OperatingUnitForm();
        operatingUnitForm.setModifierId(employee.getEmployeeId());
        operatingUnitForm.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        // 获取当前关联Id{操作单元 或者 一站一案}判断是否为空！设置表单Id
        if (map.get("relateId") != null && !"".equals(map.get("relateId"))) {
            operatingUnitForm.setCellId(map.get("relateId").toString());
            //查詢版本是否有為null的
            OperatingUnitForm relateId = findoperatingUnitForm(map.get("relateId").toString(), null,"");
            if(relateId==null)
            {
                cloneData=true;
            }
        }
        // 获取当前表单名称， 并设置表单名称
        if (map.get("cellFormName") != null && !"".equals(map.get("cellFormName"))) {
                String cellFormName = map.get("cellFormName").toString();
                operatingUnitForm.setCellFormName(cellFormName); // 设置操作表单名称 ， 需要在遍历数据的时候进行修改
        }
        //表单修改操作
        if(map.get("cellFormId") != null && !"".equals(map.get("cellFormId"))){
            if(cloneData){//克隆上個版本的數據
                operatingUnitForm.setCellFormId(UniqueUtil.uuid());
                operatingUnitForm.setCreatorId(employee.getEmployeeId());
                operatingUnitForm.setVersion(null);
                operatingUnitForm.setStatus((short) 0);
                operatingUnitForm.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                operatingUnitFormMapper.insertSelective(operatingUnitForm);
            }else{
                operatingUnitForm.setCellFormId(map.get("cellFormId").toString());
                operatingUnitFormMapper.updateByPrimaryKeySelective(operatingUnitForm);
            }
        }else{//表单新增操作
            operatingUnitForm.setCellFormId(UniqueUtil.uuid());
            operatingUnitForm.setStatus((short) 0);
            operatingUnitForm.setCreatorId(employee.getEmployeeId());
            operatingUnitForm.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            operatingUnitFormMapper.insertSelective(operatingUnitForm);
        }
        // 新增操作分类
        resInt = addOperatingUnitFormFather(map, operatingUnitForm.getCellFormId(), employee,cloneData);
        return resInt;
    }

    /**
     * 操作单元 和 一站一案
     * 操作表单配置
     *@param isStatus 当前状态
     * @param relateId 操作单元ID 或者 一站一案Id
     * @param version  版本号
     *                 用于查看  和  修改的操作
     *
     */
    @Override
    public OperatingUnitForm findoperatingUnitForm(String relateId, BigDecimal version,String isStatus) throws Exception {
        OperatingUnitForm queryOperatingUnitForm = new OperatingUnitForm();
        OperatingUnitForm operatingUnitForm=null;
        if (!"".equals(relateId) && relateId != null) {
            //通过工作内容Id查询表单配置
            queryOperatingUnitForm.setCellId(relateId);
            if("details".equals(isStatus)){//查看页面
                queryOperatingUnitForm.setVersion(version);
                operatingUnitForm = operatingUnitFormMapper.selectBycellId(queryOperatingUnitForm);
            }else{
                queryOperatingUnitForm.setVersion(null);
                operatingUnitForm = operatingUnitFormMapper.selectBycellId(queryOperatingUnitForm);
                if(operatingUnitForm==null){
                    queryOperatingUnitForm.setVersion(version);
                    operatingUnitForm = operatingUnitFormMapper.selectBycellId(queryOperatingUnitForm);
                }
            }
            if (operatingUnitForm != null && operatingUnitForm.getCellFormId() != null && !"".equals(operatingUnitForm.getCellFormId())) {
                //通过表单配置信息ID查询表单项数据
                List<com.kingyun.gpsinspection.purificationservice.facade.model.jsm.OperatingUnitFormFather> oufflist = jsmoperatingUnitFormFatherMapper.selectByPrimaryKey(operatingUnitForm.getCellFormId());
                if (oufflist != null && oufflist.size() > 0) {
                    for (OperatingUnitFormFather li : oufflist) {
                        if (li.getCellFormItemFatherId() != null && li.getCellFormItemFatherId() != "") {
                            //通过表单项数据ID查询数据项
                            List<OperatingUnitFormItem> oufi = jsmJsmOperatingUnitFormItemMapper.selectByPrimaryKey(li.getCellFormItemFatherId());
                            if (oufi != null && oufi.size() > 0) {
                                for (OperatingUnitFormItem ou : oufi) {
                                    if (ou.getCellFormItemId() != null && ou.getCellFormItemId() != "") {
                                        //通过数据项id查询多选框内容
                                        /**
                                         * 根据数据项值进行分类处理
                                         */
                                        if (Integer.parseInt(DataItemTypeEnum.MULTISELECT.getCode()) == ou.getCellFormItemType()) {
                                            List<CheckboxType> ct = checkboxTypeMapper.selectByPrimaryKey(ou.getCellFormItemId());
                                            List<String> checkBoxId = new ArrayList<>();
                                            List<String> checkBoxName = new ArrayList<>();

                                            if (ct != null && ct.size() > 0) {
                                                for (CheckboxType ctype : ct) {
                                                    checkBoxId.add(ctype.getCheckboxTypeId());
                                                    checkBoxName.add(ctype.getCheckboxTypeName());
                                                }
                                            }
                                            ou.setCheckboxId(JSON.toJSONString(checkBoxId, true));
                                            ou.setCheckboxNames(JSON.toJSONString(checkBoxName, true));

                                        } else if (Integer.parseInt(DataItemTypeEnum.RADIO.getCode()) == ou.getCellFormItemType()) {
                                            //通过数据项id查询单项框内容
                                            List<RadioType> rt = radioTypeMapper.findradioType(ou.getCellFormItemId());
                                            List<String> radioIdList = new ArrayList<>();
                                            List<String> radioNameList = new ArrayList<>();
                                            if (rt != null && rt.size() > 0) {
                                                for (RadioType radioType : rt) {
                                                    radioIdList.add(radioType.getRadioTypeId());
                                                    radioNameList.add(radioType.getRadioTypeName());
                                                }
                                                ou.setRadioNames(JSON.toJSONString(radioNameList, true));
                                                ou.setRadioTypeId(JSON.toJSONString(radioIdList, true));
                                            }
                                        }

                                        /**
                                         * 根据数据项值进行分类处理
                                         */


                                    }
                                }
                                li.setOperatingUnitFormItem(oufi);
                            }
                        }
                    }
                    operatingUnitForm.setOperatingUnitFormFather(oufflist);
                }
            }
        }
        return operatingUnitForm;
    }

    /**
     * 删除旧的工作内容配置的表单项
     *
     * @param workInstanceId
     */
    public int deleteOperatingUnitForm(String workInstanceId) {
        //查询表单项数据
        OperatingUnitForm queryOperatingUnitForm=new OperatingUnitForm();
        queryOperatingUnitForm.setCellId(workInstanceId);
        OperatingUnitForm operatingUnitForm = operatingUnitFormMapper.selectBycellId(queryOperatingUnitForm);
        if (operatingUnitForm != null && operatingUnitForm.getCellFormId() != null && !"".equals(operatingUnitForm.getCellFormId())) {
            List<OperatingUnitFormFather> oufflist = operatingUnitFormFatherMapper.selectByPrimaryKey(operatingUnitForm.getCellFormId());
            if (oufflist.size() > 0) {
                for (OperatingUnitFormFather li : oufflist) {
                    if (li.getCellFormItemFatherId() != null && li.getCellFormItemFatherId() != "") {
                        List<OperatingUnitFormItem> oufi = jsmJsmOperatingUnitFormItemMapper.selectByPrimaryKey(li.getCellFormItemFatherId());
                        if (oufi.size() > 0) {
                            for (OperatingUnitFormItem ou : oufi) {
                                if (ou.getCellFormItemId() != null && ou.getCellFormItemId() != "") {
                                    //删除多选框数据
                                    List<CheckboxType> ct = checkboxTypeMapper.selectByPrimaryKey(ou.getCellFormItemId());
                                    if (ct.size() > 0) {
                                        for (CheckboxType c : ct) {
                                            checkboxTypeMapper.deleteByPrimaryKey(c.getCellFormItemId());
                                        }
                                    }
                                    //删除单选框数据
                                    List<RadioType> rt = radioTypeMapper.findradioType(ou.getCellFormItemId());
                                    if (rt.size() > 0) {
                                        for (RadioType r : rt) {
                                            radioTypeMapper.deleteByPrimaryKey(r.getDataItemId());
                                        }
                                    }
                                    //删除数据项数据
                                    jsmJsmOperatingUnitFormItemMapper.deleteByPrimaryKey(ou.getCellFormItemFatherId());
                                }
                            }
                        }
                        //删除表单项数据
                        operatingUnitFormFatherMapper.deleteByPrimaryKey(li.getCellFormItemFatherId());
                    }
                }
            }
            //删除表单数据
            operatingUnitFormMapper.deleteByPrimaryKey(operatingUnitForm.getCellFormId());
        }
        return 0;
    }


    /**
     * 修改配置工作内容表单
     *
     * @param map
     * @return
     * @throws Exception
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateOperatingUnitForm(Map map, Employee employee) throws Exception {
        if (map.get("cellFormId").toString() != null && map.get("cellFormId").toString() != "") {
            OperatingUnitForm operatingUnitForm = operatingUnitFormMapper.selectByPrimaryKeys(map.get("cellFormId").toString());
            if (operatingUnitForm != null) {
                operatingUnitForm.setCellFormName(map.get("cellFormName").toString());
                operatingUnitForm.setModifierId(employee.getEmployeeId());
                operatingUnitForm.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                operatingUnitFormMapper.updateByPrimaryKeySelective(operatingUnitForm);
                if (map.get("operatingUnitFormFather") != null && map.get("operatingUnitFormFather") != "") {
                    List<OperatingUnitFormFather> operatingUnitFormFather = JSON.parseArray(map.get("operatingUnitFormFather").toString(), OperatingUnitFormFather.class);
                    //如果表单项id不为空就删除，否就新增
                    operatingUnitFormFatherMapper.deleteByPrimaryKey(map.get("cellFormId").toString());
                    for (OperatingUnitFormFather li : operatingUnitFormFather) {
                        //如果表单项ID不为空，就删掉以前的旧数据，在新增表单项数据
                        if (li.getCellFormItemFatherId() != null && li.getCellFormItemFatherId() != "") {
                            jsmJsmOperatingUnitFormItemMapper.deleteByPrimaryKey(li.getCellFormItemFatherId());
                        }
                        OperatingUnitFormFather ouff = new OperatingUnitFormFather();
                        if (li.getCellFormItemFatherName() != null && li.getCellFormItemFatherName() != "") {
                            ouff.setCellFormItemFatherName(li.getCellFormItemFatherName());
                        }
                        ouff.setCellFormItemFatherId(UniqueUtil.uuid());
                        ouff.setCellFormId(map.get("cellFormId").toString());
                        ouff.setCreatorId(employee.getEmployeeId());
                        ouff.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                        ouff.setModifierId(employee.getEmployeeId());
                        ouff.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                        ouff.setStatus((short) 0);
                        int b = operatingUnitFormFatherMapper.insertSelective(ouff);
                        //处理数据项表
                        updateOperatingUnitFormItem(li.getOperatingUnitFormItem(), ouff.getCellFormId(), ouff.getCellFormItemFatherId(), employee);
                    }
                }
            }
        }
        return 0;
    }

    /**
     * 新增表单项
     * 操作单元  或者 一站一案 的操作表单
     *
     * @param cellFormId 操作表单Id
     * @param map        组合数据{操作分类}
     */
    public int addOperatingUnitFormFather(Map map, String cellFormId, Employee employee,boolean cloneData) {
        int resInt = 0;
        if (map.get("operatingUnitFormFather") != null && map.get("operatingUnitFormFather") != "") {
            List<OperatingUnitFormFather> operatingUnitFormFather = JSON.parseArray(map.get("operatingUnitFormFather").toString(), OperatingUnitFormFather.class);
            if(operatingUnitFormFather!=null&&operatingUnitFormFather.size()>0){
                for (OperatingUnitFormFather li : operatingUnitFormFather) {
                    int b=0;
                    OperatingUnitFormFather ouff = new OperatingUnitFormFather();
                    // 判断操作分类 ， 并设置操作分类名称
                    if (li.getCellFormItemFatherName() != null && li.getCellFormItemFatherName() != "") {
                        ouff.setCellFormItemFatherName(li.getCellFormItemFatherName());

                    }
                    ouff.setCellFormId(cellFormId);
                    ouff.setModifierId(employee.getEmployeeId());
                    ouff.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                    if(Strings.isNullOrEmpty(li.getCellFormItemFatherId())){ //新增操作
                        ouff.setCellFormItemFatherId(UniqueUtil.uuid());
                        ouff.setCreatorId(employee.getEmployeeId());
                        ouff.setStatus((short) 0);
                        ouff.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                        /**
                         * 操作分类新增插入
                         */
                        b= operatingUnitFormFatherMapper.insertSelective(ouff);
                    }else{
                        if(cloneData){//克隆操作
                            ouff.setCellFormItemFatherId(UniqueUtil.uuid());
                            ouff.setCreatorId(employee.getEmployeeId());
                            ouff.setStatus((short) 0);
                            ouff.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                            /**
                             * 操作分类新增插入
                             */
                            b= operatingUnitFormFatherMapper.insertSelective(ouff);
                        }else{
                            //修改操作
                            ouff.setCellFormItemFatherId(li.getCellFormItemFatherId());
                            b=operatingUnitFormFatherMapper.updateByPrimaryKeySelective(ouff);
                        }
                    }
                    /**
                     * 操作表单数据项新增插入
                     */
                    resInt = b + addOperatingUnitFormItem(li.getOperatingUnitFormItem(), ouff.getCellFormId(), ouff.getCellFormItemFatherId(), employee,cloneData);

                }
            }
        }
        return resInt;
    }

    /**
     * 操作单元 或者 一站一案 表单项中
     * 数据项新增
     *
     * @param list                 数据项组合数据
     * @param cellFormId           操作表单Id
     * @param cellFormItemFatherId 操作分类Id
     *                             备注：数据项新增
     */
    public int addOperatingUnitFormItem(List<OperatingUnitFormItem> list, String cellFormId, String cellFormItemFatherId, Employee employee,boolean cloneData) {
        int a = 0;
        // 判断数据项非空判断
        if (list != null && list.size() > 0) {
            for (OperatingUnitFormItem lis : list) {
                int b=0;
                OperatingUnitFormItem operatingUnitFormItem = new OperatingUnitFormItem();
                //数据项名称非空判断
                if (lis.getCellFormItemName() != null && lis.getCellFormItemName() != "") {
                    operatingUnitFormItem.setCellFormItemName(lis.getCellFormItemName());
                }
                //数据项类型非空判断
                if (lis.getCellFormItemType() != null) {
                    operatingUnitFormItem.setCellFormItemType(lis.getCellFormItemType());
                }
                //数据项多选单选非空判断
                if (lis.getCellFormItemUnit() != null && lis.getCellFormItemUnit() != "") {
                    operatingUnitFormItem.setCellFormItemUnit(lis.getCellFormItemUnit());
                }
                //数据项最大值非空判断
                if (lis.getMaximum() != null && lis.getMaximum() != "") {
                    operatingUnitFormItem.setMaximum(lis.getMaximum());
                }
                //数据项最小值非空判断
                if (lis.getMinimum() != null && lis.getMinimum() != "") {
                    operatingUnitFormItem.setMinimum(lis.getMinimum());
                }
                //数据项实体非空判断
                if (lis.getEntityId() != null && lis.getEntityId() != "") {
                    operatingUnitFormItem.setEntityId(lis.getEntityId());
                }
                //数据项生产实体类型非空判断
                if (lis.getEntityTypeId() != null && lis.getEntityTypeId() != "") {
                    operatingUnitFormItem.setEntityTypeId(lis.getEntityTypeId());
                }

                //设备类型非空判断
                if(lis.getEntityId() != null && lis.getEntityId() != ""){
                    operatingUnitFormItem.setEntityId(lis.getEntityId());
                }
                operatingUnitFormItem.setCellFormItemType(lis.getCellFormItemType());
                operatingUnitFormItem.setDataType(lis.getDataType());
                operatingUnitFormItem.setPropertyId(lis.getPropertyId());
                operatingUnitFormItem.setCellFormId(cellFormId);
                operatingUnitFormItem.setCellFormItemFatherId(cellFormItemFatherId);
                operatingUnitFormItem.setModifierId(employee.getEmployeeId());
                operatingUnitFormItem.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                if(Strings.isNullOrEmpty(lis.getCellFormItemId())){//新增操作
                    operatingUnitFormItem.setCellFormItemId(UniqueUtil.uuid());
                    operatingUnitFormItem.setCreatorId(employee.getEmployeeId());
                    operatingUnitFormItem.setStatus((short) 0);
                    operatingUnitFormItem.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                    /**
                     * 数据项新增插入
                     */
                    b = jsmJsmOperatingUnitFormItemMapper.insertSelective(operatingUnitFormItem);
                }else{
                    if(cloneData){//克隆操作
                        operatingUnitFormItem.setCellFormItemId(UniqueUtil.uuid());
                        operatingUnitFormItem.setCreatorId(employee.getEmployeeId());
                        operatingUnitFormItem.setStatus((short) 0);
                        operatingUnitFormItem.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                        /**
                         * 数据项新增插入
                         */
                        b = jsmJsmOperatingUnitFormItemMapper.insertSelective(operatingUnitFormItem);
                    }else{
                        //修改操作
                        operatingUnitFormItem.setCellFormItemId(lis.getCellFormItemId());
                        b = jsmJsmOperatingUnitFormItemMapper.updateByPrimaryKeySelective(operatingUnitFormItem);
                    }
                }
                // 保存数据项中的单选框类型
                addRadioTypeName(lis.getRadioNames(), operatingUnitFormItem.getCellFormItemId(), employee,cloneData);
                // 保存数据项中的复选框类型
                a = b + addCheckboxType(lis.getCheckboxNames(), operatingUnitFormItem.getCellFormItemId(), employee,cloneData);
            }
        }
        return a;
    }

    /**
     * 新增复选框类型
     * 操作单元 或者 一站一案表单
     *
     * @param checkboxName   多选框值插入
     * @param cellFormItemId 数据项Id
     */
    public int addCheckboxType(String checkboxName, String cellFormItemId, Employee employee,boolean cloneData) {
        List<String> checkboxNames = JSON.parseArray(checkboxName, String.class);
        int resInt = 0;
        if(!cloneData){
            checkboxTypeMapper.deleteByFormItemId(cellFormItemId);
        }
        /**
         * 多选框非空判断
         */
        if (checkboxNames != null && checkboxNames.size() > 0) {
            for (int i = 0; i < checkboxNames.size(); i++) {
                CheckboxType checkboxType = new CheckboxType();
                checkboxType.setCheckboxTypeId(UniqueUtil.uuid());
                checkboxType.setCheckboxTypeName(checkboxNames.get(i));
                checkboxType.setCreatorId(employee.getEmployeeId());
                checkboxType.setCreateTime(new Date());
                checkboxType.setModifierId(employee.getEmployeeId());
                checkboxType.setModifyTime(new Date());
                checkboxType.setStatus((short) 0);
                checkboxType.setCellFormItemId(cellFormItemId);
                /**
                 * 多选框值插入
                 */
                resInt = +checkboxTypeMapper.insertSelective(checkboxType);
            }
        }
        return resInt;
    }

    /**
     * 新增单选框类型
     *
     * @param radioTypeName  单选框名称
     * @param cellFormItemId 数据项Id
     */
    public int addRadioTypeName(String radioTypeName, String cellFormItemId, Employee employee,boolean cloneData) {
        List<String> radioTypes = JSON.parseArray(radioTypeName, String.class);
        if(!cloneData){
            radioTypeMapper.deleteByFormItemId(cellFormItemId);
        }
        int a = 0;
        /**
         * 单选框非空判断！
         */
        if (radioTypes != null && radioTypes.size() > 1) {
            for (int i = 0; i < radioTypes.size(); i++) {
                RadioType radioType = new RadioType();
                radioType.setRadioTypeId(UniqueUtil.uuid());
                radioType.setRadioTypeName(radioTypes.get(i));
                radioType.setCreatorId(employee.getEmployeeId());
                radioType.setCreateTime(new Date());
                radioType.setModifierId(employee.getEmployeeId());
                radioType.setModifyTime(new Date());
                radioType.setStatus((short) 0);
                radioType.setDataItemId(cellFormItemId);
                a = +radioTypeMapper.insertSelective(radioType);
            }
        }
        return a;
    }


    /**
     * 修改数据项
     *
     * @param list
     * @param cellFormId
     * @param cellFormItemFatherId
     * @return
     */
    public int updateOperatingUnitFormItem(List<OperatingUnitFormItem> list, String cellFormId, String cellFormItemFatherId, Employee employee) {
        int a = 0;
        if (list != null && list.size() > 0) {
            for (OperatingUnitFormItem lis : list) {
                //先删除原来数据项的复选框
                if (lis.getCellFormItemId() != null && lis.getCellFormItemId() != "") {
                    checkboxTypeMapper.deleteByPrimaryKey(lis.getCellFormItemId());
                }
                OperatingUnitFormItem oufi = new OperatingUnitFormItem();
                if (lis.getCellFormItemName() != null && lis.getCellFormItemName() != "") {
                    oufi.setCellFormItemName(lis.getCellFormItemName());
                }
                if (lis.getCellFormItemType() != null) {
                    oufi.setCellFormItemType(lis.getCellFormItemType());
                }
                if (lis.getCellFormItemUnit() != null && lis.getCellFormItemUnit() != "") {
                    oufi.setCellFormItemUnit(lis.getCellFormItemUnit());
                }
                if (lis.getMaximum() != null && lis.getMaximum() != "") {
                    oufi.setMaximum(lis.getMaximum());
                }
                if (lis.getMinimum() != null && lis.getMinimum() != "") {
                    oufi.setMinimum(lis.getMinimum());
                }
                if (lis.getRadioTypeId() != null && lis.getRadioTypeId() != "") {
                    oufi.setRadioTypeId(lis.getRadioTypeId());
                }
                oufi.setCellFormItemId(UniqueUtil.uuid());
                oufi.setCellFormId(cellFormId);
                oufi.setCellFormItemFatherId(cellFormItemFatherId);
                oufi.setCreatorId(employee.getEmployeeId());
                oufi.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                oufi.setModifierId(employee.getEmployeeId());
                oufi.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                oufi.setStatus((short) 0);
                int b = jsmJsmOperatingUnitFormItemMapper.insertSelective(oufi);
                updateCheckboxType(lis.getCheckboxNames(), oufi.getCellFormItemId(), employee);
            }
        }
        return a;
    }

    /**
     * 修改复选框
     *
     * @param checkboxName
     * @param cellFormItemId
     * @return
     */
    public int updateCheckboxType(String checkboxName, String cellFormItemId, Employee employee) {
        int a = addCheckboxType(checkboxName, cellFormItemId, employee,false);
        return a;
    }

    public List<CheckboxType> findCheckboxTypeName(String checkboxName, String cellFormItemId) {
        List<CheckboxType> list = checkboxTypeMapper.selectCheckboxTypeList(checkboxName, cellFormItemId);
        return list;
    }


    /**
     * @param version    根据版本
     * @param isWorkCell 是否是一站一案 {true:操作单元 false 一站一案}
     *                   1：查询当前表单
     *                   2：查询当前分类
     *                   3：查询当前数据项
     *                   4：查询数据项值{
     *                   单选框 和 复选框
     *                   }
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int cloneOperateForm(Version version, boolean isWorkCell, BigDecimal oldVersion, EmployeeVo employee) throws Exception {
        int resInt = 0;
        // 查询存在无版本的操作表单
        OperatingUnitForm versionOperate = new OperatingUnitForm();
        versionOperate.setCellId(version.getRelatedId());
        // 查询当前表单是否存在 如果存在 则直接保存 ；
        OperatingUnitForm operatingUnitForm = operatingUnitFormMapper.selectBycellId(versionOperate);
        // 如果当前不存在表单情况则进行表单复制
        if (operatingUnitForm == null || operatingUnitForm.getCellFormId() == null) {
            versionOperate.setCellId(version.getRelatedId());
            versionOperate.setVersion(oldVersion);
            operatingUnitForm = operatingUnitFormMapper.selectBycellId(versionOperate);
            if (operatingUnitForm != null && operatingUnitForm.getCellFormId() != null) {
                String oldFormId = operatingUnitForm.getCellFormId();
                String creatorId = employee.getEmployeeId();
                operatingUnitForm.setCellFormId(UniqueUtil.uuid());
                operatingUnitForm.setModifierId(creatorId);
                operatingUnitForm.setCreatorId(creatorId);
                operatingUnitForm.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                operatingUnitForm.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                operatingUnitForm.setVersion(version.getVersionNumber());
                this.saveOperatingUnitForm(operatingUnitForm);

                /**
                 * 查询当前表单的表单分类
                 */
                List<OperatingUnitFormFather> operatingUnitFormFatherList =
                        operatingUnitFormFatherMapper.selectByPrimaryKey(oldFormId);
                if (operatingUnitFormFatherList != null && operatingUnitFormFatherList.size() > 0) {
                    for (OperatingUnitFormFather offather : operatingUnitFormFatherList) {
                        String oldFormFather = offather.getCellFormItemFatherId();
                        offather.setCellFormId(operatingUnitForm.getCellFormId()); // 设置新的表单信息
                        offather.setModifierId(creatorId);
                        offather.setCreatorId(creatorId);
                        offather.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                        offather.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                        offather.setCellFormItemFatherId(UniqueUtil.uuid());
                        resInt = +operatingUnitFormFatherMapper.insertSelective(offather);
                        /**
                         * 查询数据项 问题
                         * oldFormFather 查询老的东西
                         */

                        List<OperatingUnitFormItem> operatingUnitFormItems = jsmJsmOperatingUnitFormItemMapper.selectByPrimaryKey(oldFormFather);
                        //判断特性值是否绑定了设备
                        if(isWorkCell==false&&unBindDevice(operatingUnitFormItems)){
                            resInt=-1;
                            return resInt;
                        }
                        if (operatingUnitFormItems != null && operatingUnitFormItems.size() > 0) {
                            for (OperatingUnitFormItem operatingUnitFormItem : operatingUnitFormItems) {
                                String oldOperatingUnitFormItem = operatingUnitFormItem.getCellFormItemId();
                                operatingUnitFormItem.setCellFormItemFatherId(offather.getCellFormItemFatherId());
                                operatingUnitFormItem.setCellFormId(oldFormId);
                                operatingUnitFormItem.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                                operatingUnitFormItem.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                                operatingUnitFormItem.setCellFormItemId(UniqueUtil.uuid());
                                resInt = +jsmJsmOperatingUnitFormItemMapper.insertSelective(operatingUnitFormItem);
                                /**
                                 * 根据数据项类型 查询 单选内容 和  复选内容
                                 */
                                List<RadioType> radioTypes = radioTypeMapper.findradioType(oldOperatingUnitFormItem);
                                if (radioTypes != null && radioTypes.size() > 0) {
                                    for (RadioType radioType : radioTypes) {
                                        radioType.setDataItemId(operatingUnitFormItem.getCellFormItemId());
                                        radioType.setRadioTypeId(UniqueUtil.uuid());
                                        radioType.setCreatorId(creatorId);
                                        radioType.setModifierId(creatorId);
                                        radioType.setCreateTime(new Date());
                                        radioType.setModifyTime(new Date());
                                        resInt = +radioTypeMapper.insertSelective(radioType);
                                    }
                                }
                                /**
                                 * 根据数据项类型 查询 单选内容 和  复选内容
                                 */
                                List<CheckboxType> checkboxTypes = checkboxTypeMapper.selectByPrimaryKey(oldOperatingUnitFormItem);
                                if (checkboxTypes != null && checkboxTypes.size() > 0) {
                                    for (CheckboxType checkboxType : checkboxTypes) {
                                        checkboxType.setDataItemId(operatingUnitFormItem.getCellFormItemId());
                                        checkboxType.setCellFormItemId(operatingUnitFormItem.getCellFormItemId());
                                        checkboxType.setCheckboxTypeId(UniqueUtil.uuid());
                                        checkboxType.setCreatorId(creatorId);
                                        checkboxType.setModifierId(creatorId);
                                        checkboxType.setCreateTime(new Date());
                                        checkboxType.setModifyTime(new Date());
                                        resInt = +checkboxTypeMapper.insert(checkboxType);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            List<OperatingUnitFormFather> operatingUnitFormFatherList = operatingUnitFormFatherMapper.selectByPrimaryKey(operatingUnitForm.getCellFormId());
            if (operatingUnitFormFatherList != null && operatingUnitFormFatherList.size() > 0) {
                for (OperatingUnitFormFather offather : operatingUnitFormFatherList) {
                    List<OperatingUnitFormItem> operatingUnitFormItems = jsmJsmOperatingUnitFormItemMapper.selectByPrimaryKey(offather.getCellFormItemFatherId());
                    if(isWorkCell==false&&unBindDevice(operatingUnitFormItems)){
                        resInt=-1;
                        return resInt;
                    }
                }}
            operatingUnitForm.setVersion(version.getVersionNumber());
            operatingUnitFormMapper.updateByPrimaryKeySelectiveIsNull(operatingUnitForm); // 更新表单问题
        }
        return resInt;
    }

    /**
     * 判断特性是否绑定了设备
     * @param operatingUnitFormItems
     * @return
     */
    private boolean unBindDevice( List<OperatingUnitFormItem> operatingUnitFormItems){
        boolean flag=false;
        if (operatingUnitFormItems != null && operatingUnitFormItems.size() > 0) {
            for (OperatingUnitFormItem operatingUnitFormItem : operatingUnitFormItems) {
                BigDecimal dataType = operatingUnitFormItem.getDataType();
                if(dataType!=null){
                    if(1==dataType.intValue()){
                        if(Strings.isNullOrEmpty( operatingUnitFormItem.getEntityId())){
                            flag=true;
                            break;
                        }

                    }
                }
            }
        }
        return flag;
    }


    /**
     * @param fromWorkCellInfo   操作单元
     * @param toWorkCellInstance 一站一案
     *                           保存一站一案 将表单复制到一站一案
     */
    @Override
    public Map<String, Object> cloneOperateForm(WorkCellInfo fromWorkCellInfo, WorkCellInstance toWorkCellInstance, EmployeeVo employee) {
        int resInt = 0;
        Map<String, Object> stringObjectMap = new HashMap<>();
        /**
         * 查询当前表单
         */
        // 判断如果是操作单元 或者 一站一案则进行保存问题
        OperatingUnitForm versionOperate = new OperatingUnitForm();
        versionOperate.setCellId(fromWorkCellInfo.getCellId());
        versionOperate.setVersion(fromWorkCellInfo.getStepVersion());
        // 查询当前表单是否存在 如果存在 则直接保存 ；
        OperatingUnitForm operatingUnitForm = operatingUnitFormMapper.selectBycellId(versionOperate);
        if (operatingUnitForm != null && operatingUnitForm.getCellFormId() != null) {
            String oldFormId = operatingUnitForm.getCellFormId();
            String creatorId = employee.getEmployeeId();
            operatingUnitForm.setCellFormId(UniqueUtil.uuid());
            operatingUnitForm.setModifierId(creatorId);
            operatingUnitForm.setCreatorId(creatorId);
            operatingUnitForm.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            operatingUnitForm.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            operatingUnitForm.setCellId(toWorkCellInstance.getWorkInstanceId());
            operatingUnitForm.setVersion(null);
            try {
                this.saveOperatingUnitForm(operatingUnitForm);
            } catch (Exception e) {
                e.printStackTrace();
            }
            /**
             * 查询当前表单的表单分类
             */
            List<OperatingUnitFormFather> operatingUnitFormFatherList =
                    operatingUnitFormFatherMapper.selectByPrimaryKey(oldFormId);
            // 查询表单分类项
            if (operatingUnitFormFatherList != null && operatingUnitFormFatherList.size() > 0) {
                // 循环表单分类项
                for (OperatingUnitFormFather offather : operatingUnitFormFatherList) {
                    String oldFormFather = offather.getCellFormItemFatherId();
                    offather.setCellFormId(operatingUnitForm.getCellFormId()); // 设置新的表单信息
                    offather.setModifierId(creatorId);
                    offather.setCreatorId(creatorId);
                    offather.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                    offather.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                    offather.setCellFormItemFatherId(UniqueUtil.uuid());
                    resInt = +operatingUnitFormFatherMapper.insertSelective(offather);
                    /**
                     * 查询数据项 问题
                     * oldFormFather 查询老的东西
                     */

                    List<OperatingUnitFormItem> operatingUnitFormItems = jsmJsmOperatingUnitFormItemMapper.selectByPrimaryKey(oldFormFather);
                    if (operatingUnitFormItems != null && operatingUnitFormItems.size() > 0) {
                        for (OperatingUnitFormItem operatingUnitFormItem : operatingUnitFormItems) {

                            String oldOperatingUnitFormItem = operatingUnitFormItem.getCellFormItemId();

                            operatingUnitFormItem.setCellFormItemFatherId(offather.getCellFormItemFatherId());
                            operatingUnitFormItem.setCellFormId(operatingUnitForm.getCellFormId());
                            operatingUnitFormItem.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                            operatingUnitFormItem.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                            operatingUnitFormItem.setCellFormItemId(UniqueUtil.uuid());
                            resInt = +jsmJsmOperatingUnitFormItemMapper.insertSelective(operatingUnitFormItem);
                            /**
                             * 根据数据项类型 查询 单选内容 和  复选内容
                             */
                            List<RadioType> radioTypes = radioTypeMapper.findradioType(oldOperatingUnitFormItem);
                            if (radioTypes != null && radioTypes.size() > 0) {
                                for (RadioType radioType : radioTypes) {
                                    radioType.setDataItemId(operatingUnitFormItem.getCellFormItemId());
                                    radioType.setRadioTypeId(UniqueUtil.uuid());
                                    radioType.setCreatorId(creatorId);
                                    radioType.setModifierId(creatorId);
                                    radioType.setCreateTime(new Date());
                                    radioType.setModifyTime(new Date());
                                    resInt = +radioTypeMapper.insertSelective(radioType);
                                }
                            }
                            /**
                             * 根据数据项类型 查询 单选内容 和  复选内容
                             */
                            List<CheckboxType> checkboxTypes = checkboxTypeMapper.selectByPrimaryKey(oldOperatingUnitFormItem);
                            if (checkboxTypes != null && checkboxTypes.size() > 0) {
                                for (CheckboxType checkboxType : checkboxTypes) {
                                    checkboxType.setDataItemId(operatingUnitFormItem.getCellFormItemId());
                                    checkboxType.setCellFormItemId(operatingUnitFormItem.getCellFormItemId());
                                    checkboxType.setCheckboxTypeId(UniqueUtil.uuid());
                                    checkboxType.setCreatorId(creatorId);
                                    checkboxType.setModifierId(creatorId);
                                    checkboxType.setCreateTime(new Date());
                                    checkboxType.setModifyTime(new Date());
                                    resInt = +checkboxTypeMapper.insertSelective(checkboxType);
                                }
                            }
                        }
                    }
                }
            }
        }
        return stringObjectMap;
    }

    /**
     * 删除数据分类
     *
     * @param formFatherId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer deleteFormfather(String formFatherId) {
        int a=0;
        List<OperatingUnitFormItem> operatingUnitFormItems = jsmJsmOperatingUnitFormItemMapper.queryByFatherFormId(formFatherId);
        for(OperatingUnitFormItem ouf:operatingUnitFormItems){
            radioTypeMapper.deleteByFormItemId(ouf.getCellFormItemId());
            checkboxTypeMapper.deleteByFormItemId(ouf.getCellFormItemId());
        }
        jsmJsmOperatingUnitFormItemMapper.deleteByFatherFormId(formFatherId);
        a=operatingUnitFormFatherMapper.deleteByPid(formFatherId);
        return a;
    }

    /**
     * 删除数据项
     *
     * @param formItemId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer deleteFormItem(String formItemId) {
        int a=0;
            radioTypeMapper.deleteByFormItemId(formItemId);
            checkboxTypeMapper.deleteByFormItemId(formItemId);
            a= jsmJsmOperatingUnitFormItemMapper.deleteByFormId(formItemId);
        return a;
    }

    /**
     * 新增表单
     */
    private Integer saveOperatingUnitForm(OperatingUnitForm operatingUnitForm) throws Exception {
        return operatingUnitFormMapper.insertSelective(operatingUnitForm);
    }




}
